package com.main.beatit;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.preference.*;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Preference extends PreferenceActivity {
	public static EditTextPreference epBPM;
	public static android.preference.Preference pSoundChange;
	float fOldBPM;
	private Intent iSoundChange;

	private Intent iKim;
	private Intent iHan;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		iKim = new Intent(this, ContactKim.class);
		iHan = new Intent(this, ContactHan.class);

		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		addPreferencesFromResource(R.xml.preference);

		iSoundChange = new Intent(this, SoundChange.class);

		// Preference view I got! like findViewById();
		epBPM = (EditTextPreference) findPreference("bpm");
		pSoundChange = findPreference("soundchange");

		epBPM.setSummary("BPM : " + PreferenceManager.getDefaultSharedPreferences(this).getString("bpm", "120"));
		fOldBPM = 120;

		epBPM.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

			public boolean onPreferenceChange(android.preference.Preference preference, Object newValue) {
				if (Float.parseFloat(newValue.toString()) < 60 || Float.parseFloat(newValue.toString()) > 240) {
					Toast.makeText(Preference.this, "Wrong number! Please enter between 60 to 240", Toast.LENGTH_SHORT)
							.show();

					newValue = fOldBPM;

					MainActivity.fBPM = fOldBPM;
					MainActivity.nBeat = (int) (1000 / (MainActivity.fBPM / 15));

					return false;
				}

				fOldBPM = Float.parseFloat(newValue.toString());
				epBPM.setSummary("BPM : " + newValue.toString());

				MainActivity.fBPM = fOldBPM;
				MainActivity.nBeat = (int) (1000 / (MainActivity.fBPM / 15));

				return true;
			}
		});

		pSoundChange.setOnPreferenceClickListener(new OnPreferenceClickListener() {

			public boolean onPreferenceClick(android.preference.Preference preference) {
				// TODO Auto-generated method stub

				startActivity(iSoundChange);

				return false;
			}
		});

		android.preference.Preference pContactKim = findPreference("contact_kim");
		android.preference.Preference pContactHan = findPreference("contact_han");

		pContactKim.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			
			public boolean onPreferenceClick(android.preference.Preference preference) {
				try {
					startActivity(iKim);
				} catch (Exception e) {
					Toast.makeText(Preference.this, e.getMessage(), Toast.LENGTH_LONG).show();
				}
								
				return false;
			}
		});
		
		pContactHan.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			
			public boolean onPreferenceClick(android.preference.Preference preference) {
				try {
					startActivity(iHan);
				} catch (Exception e) {
					Toast.makeText(Preference.this, e.getMessage(), Toast.LENGTH_LONG).show();
				}
								
				return false;
			}
		});

	}
}
